import { reqLogin } from '@/api/user'
import { setToken, getToken } from '@/utils/auth'

export default {
  namespaced: true,
  state: {
    token: getToken() || ''
  },
  mutations: {
    setUserToken(state, newToken) {
      state.token = newToken
      setToken(newToken)
    }
  },
  actions: {
    login(context, data) {
      return new Promise((resolve, reject) => {
        reqLogin(data).then(res => {
          const data = res.data.data
          context.commit('setUserToken', data)
          resolve(res)
        }).catch(err => {
          reject(err)
        })
      })
    }
  },
  getters: {}
}

